
package com.lzb.findphone.adapter;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.UriUtils;
import com.lzb.findphone.R;
import com.lzb.findphone.basic.media.IAudioPlayer;
import com.lzb.findphone.bean.SoundBean;

import java.util.ArrayList;
import java.util.List;

public class RingtoneAdapter extends RecyclerView.Adapter<RingtoneAdapter.ViewHolder> {

    private List<SoundBean> mSoundBeans = new ArrayList<>();
    private IAudioPlayer mAudioPlayer;
    public IAudioPlayer mCurAudioPlayer;
    private ViewHolder mPlayHolder;
    private OnItemClickListener listener;

    public RingtoneAdapter(IAudioPlayer audioPlayer) {
        mAudioPlayer = audioPlayer;
        mAudioPlayer.setListener(mAudioPlayListener);
    }

    public void setSoundBeans(List<SoundBean> mSoundBeans) {
        this.mSoundBeans = mSoundBeans;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ringtone, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bindData(mSoundBeans.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mSoundBeans.size();
    }

    private void handlePlayPause(ViewHolder holder, int position) {
        if (mPlayHolder == holder) {
            if (mCurAudioPlayer != null && mCurAudioPlayer.isPlaying()) {
                mCurAudioPlayer.pause();
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
            return;
        }

        if (mPlayHolder != null) {
            mPlayHolder.mIvPlayPause.setImageResource(R.drawable.icon_play);
            mPlayHolder.mTvPlay.setText(mPlayHolder.itemView.getContext().getString(R.string.start_trial_listening));
        }

        if (mCurAudioPlayer != null && mCurAudioPlayer.isPlaying()) {
            mCurAudioPlayer.pause();
        }

        mPlayHolder = holder;
        Uri uri = Uri.parse("android.resource://" + AppUtils.getAppPackageName() + "/" + mSoundBeans.get(position).getRawId());
        mAudioPlayer.play(UriUtils.uri2File(uri).getPath());
    }

    private void handleApply(ViewHolder holder, int position) {
        if (mPlayHolder == holder) {
            mAudioPlayer.stop();
            mPlayHolder = null;
        }
        if (listener != null) {
            listener.onItemApply(position);
        }
    }

    private IAudioPlayer.IListener mAudioPlayListener = new IAudioPlayer.IListener() {
        @Override
        public void onUpdatePlayTime(int curPlayTime, int duration) {
            if (mPlayHolder != null) {
                // 可以在这里更新播放进度
            }
        }

        @Override
        public void onPlayChange(boolean isPlaying) {
            if (mPlayHolder != null) {
                mPlayHolder.mIvPlayPause.setImageResource(isPlaying ? R.drawable.icon_stop : R.drawable.icon_play);
                mPlayHolder.mTvPlay.setText(isPlaying ? mPlayHolder.itemView.getContext().getString(R.string.stop_trial_listening) : mPlayHolder.itemView.getContext().getString(R.string.start_trial_listening));
            }
        }
    };

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTvPlay;
        public TextView mTvApply;
        private LinearLayout mLlPlay;
        public ImageView mIvImg;
        public ImageView mIvPlayPause;
        public int mBindPos;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvPlay = itemView.findViewById(R.id.tvRingtoneItemPlay);
            mTvApply = itemView.findViewById(R.id.tvRingtoneItemApply);
            mLlPlay = itemView.findViewById(R.id.llRingtoneItemPlay);
            mIvImg = itemView.findViewById(R.id.ivRingtoneItemImg);
            mIvPlayPause = itemView.findViewById(R.id.ivRingtoneItemPlay);
        }

        public void bindData(SoundBean bean, int position) {
            mBindPos = position;

            mIvImg.setImageResource(bean.getImg());

            mTvApply.setOnClickListener(v -> {
                handleApply(this, position);
            });

            mLlPlay.setOnClickListener(v -> {
                handlePlayPause(this, position);
            });
        }
    }

    public interface OnItemClickListener {
        void onItemApply(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
